<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>       
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="${_resources}/logo.png"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">                                            
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />  
<title>样式模板定义</title>
<%@ include file="/WEB-INF/jsp/common/inc.jsp" %>
<script type="text/javascript" src="${_resources}jquery-ui/jquery-ui-1.10.4.custom.min.js"></script>
<link rel="stylesheet" href="${_resources}jquery-ui/jquery-ui-1.10.4.custom.min.css" />
<style type="text/css">
body{	
	overflow:hidden;
}
</style>
<script type="text/javascript">
$(function(){
	$("input[type='file']").change( function() {
		var fileType =$("#textfield").val();
		$(".upload_text").text(fileType);
		$(".upload_text").attr("title",fileType);
		$(".upload_delete").click(function(){
			$(".upload_text").val("");
			$(".upload_text").text("");
			$("#textfield").val("");
			$(".upload_text").attr("title","");
			});
		});
	})
$(function(){
	$("#form").validationEngine({
		autoHidePrompt:true,//自动隐藏
		showOnMouseOver:true,//当鼠标移出时触发
		promptPosition:'bottomLeft',//提示信息的位置
		inlineValidation: true,//是否即时验证，false为提交表单时验证,默认true  
		failure : function() {
		    layer.message("验证失败，请检查");
			return false;  
		},//验证失败时调用的函数
		ajaxFormValidation: true,//开始AJAX验证
		success : function() {
		//	$("#addForm").submit();
		}//验证通过时调用的函数 
		//onAjaxFormComplete: ajaxValidationCallback
	});
	$("#textfield").change(function (){
		var path = $("#textfield").val();
		var pos1 = path.lastIndexOf('/');
		var pos2 = path.lastIndexOf('\\');
		var pos = Math.max(pos1, pos2);
		var fileName;
		if( pos<0 )
			fileName = path;
		else
			fileName = path.substring(pos+1); 
		fileName = fileName.substring(0,fileName.lastIndexOf("."));
		$("#styleDocumentName").val(fileName);
		judgeName();
	});
});
function formSubmit(){
	verifyName();
	if(judgeFileType()){
		var options = {
			type: "POST",    
			async: false, 	
			dataType:"json",
			success:function(json){
				if(json.code == "1"){
					layer.msg("格式不符",{shift:5,time:1500},function(){
						parent.closeStyleTempDiv();
						return false;
					});
				}
				if(json.code == "0"){
					layer.msg("保存成功",{shift:5,time:1500},function(){
						parent.closeStyleTempDiv();
						parent.refreshTable();
					});
				}
			},
			error:function(json){
				layer.msg("系统错误,请联系管理员");
				parent.closeStyleTempDiv();
				parent.refreshTable();
			}
		};
	if($("#form").validationEngine('validate')){
		layer.confirm('正在上传数据,请稍后...',{icon: 16,shade: 0.3,
			success: function(layero){
	        	 var obj = $(".layui-layer"),oClose = $(".layui-layer-close");
				 str = "<iframe  frameborder=\"0\" scrolling=\"no\" style=\"position:absolute; background-color:transparent; width:100%; height:100%; top:0px; left:0px; z-index:-1;\"></iframe>";
				 obj.prepend(str);
				 oClose.click(function(){
					    var oShade = $("#templatelist").contents().find("#shade");
						oShade.css({'width':'0','height':'0'});
						$("#popDivs").css("height","100%");
			     }) 
			     $(".layui-layer").css("background-color","#f0efef");
					$(".layui-layer-content").css("text-align","center");
					$(".layui-layer-setwin").css("display","none");
					$(".layui-layer-btn").css("display","none");
					$(".layui-layer-title").css("display","none");
					$(".layui-layer-content").css("height","70px");
	        }});
		$("#submit").attr("onclick","");
		$('#form').ajaxSubmit(options);
	}
  }
}
//验证样式输出文档
function checkSelect(){	
	var select = $("select[name='styleType']");
	var options = $(select).find("option");
	for(var i =0;i<options.length;i++){
		if($(options[i]).prop("selected")=="selected"||$(options[i]).prop("selected")==true){
			var value = $(options[i]).val();
			if(value=="请选择"){
				return "请选择模板类型";
			}else{
				return true;
			}
		}
	}
	
}
//取消和关闭调用的方法
function cancelAndClose(){
	parent.closeStyleTempDiv();
}
 function judgeName(){
	var path="${_baseUrl}/KmStyleDocumentTemplateController/judgeStyleName";
	var name = $("#styleDocumentName").val();
	var type = $("#styleType").val();
	if(name !=""){
		$.ajax({    
			type: "POST",    
			async: false,    
			url:path,
			data:{
				"styleName":name,
				"type":type
			},
			dataType:"json",    
			success: function (json) {   
				if(json.code=="1"){
					layer.msg("模板名称已经存在");
					$("#styleDocumentName").val("");
				}
			}   
		});
	}
} 
function verifyName(){
	var path="${_baseUrl}/KmStyleDocumentTemplateController/verifyName";
	var name = $("#styleDocumentName").val();
	if(name !=""){
		$.ajax({    
			type: "POST",    
			async: false,    
			url:path,
			data:{
				"styleName":name,
			},
			dataType:"json",    
			success: function (json) {   
				if(json.code=="2"){
					layer.msg("模板名称已经存在");
					$("#styleDocumentName").val("");
					return false;
				}
			}   
		});
	}
}
function judgeFileType(){	
	var fileType =$("#textfield").val();
	var typeMark =  fileType.substring(fileType.length-4,fileType.length);
	if(fileType==""){
		layer.msg("请选择doc或docx文件！");
		return false;
	}
	if(typeMark ==".doc"||typeMark ==".DOC"||(typeMark =="docx" &&fileType.indexOf("."))||(typeMark=="DOCX"&&fileType.indexOf("."))){
		return true;
	}else{
		layer.msg("请选择doc或docx文件！");
		return false;
	}
}
document.onkeydown=function(event) { 
	e = event ? event :(window.event ? window.event : null); 
	if(e.keyCode==13){ 
		return false; 
	} 
} 
</script>
<style type="text/css">
</style>
</head>
<body>
<div style="padding-bottom:5px;">
<form action="${_baseUrl}/KmStyleDocumentTemplateController/styleTemplateUpload" id="form" method="post"  enctype="multipart/form-data">
<input type="hidden" name="token" value="${token}" />
<div class="jbinformation" style="width:auto;">
    <table class="table table-bordered submenu_table">
		<tr>
        <td height="50" width="15%" align="center"><span style="color:#f00">*</span> 样式文档名称</td>
        <td width="35%" valign="middle">
	        <div>
	        <input type="text" name="styleDocumentName" id="styleDocumentName"  class="nav_input validate[required,funcCall[verifyName]]">
	        </div>
        </td>
      </tr>
      <tr>
        <td height="50" width="15%" align="center"><span style="color:#f00">*</span> 样式文档</td>
        <td width="35%" valign="middle">
        	<div class="upload">
			     <input class="upload_input" type="file" class="nav_input validate[required,funcCall[judgeFileType]" name=multipartFile id="textfield" >
			     <div class="upload_text"></div>
			     <div class="upload_text_a"></div>
			     <a class="upload_btn" href="javascript:;">上传</a>
			     <a class="upload_delete" href="javascript:;"></a>
			</div>
		</td>
      </tr>
    </table>
</div>
<div class="permission_an mubanclass_an ma mt10">
	<shiro:hasPermission name="KmStyleDocumentTemplateController:modifyStyleTemplate">
       <a href="javascript:;" id="submit" onclick="formSubmit();" class="per_baocun">提 交</a>
    </shiro:hasPermission>
	<shiro:hasPermission name="KmStyleDocumentTemplateController:close2">
    <a href="javascript:;" class="per_gbi" onclick="cancelAndClose();">关 闭</a>
    </shiro:hasPermission>
</div>

</form>
</div>
</body>
</html>